<?php
 namespace Admin\Model;
/**
 * 基础服务类
 */
use Think\Model;
class BaseModel extends Model {
    /**
     * 用来处理内容中为空的判断
     */
	public function checkEmpty($data,$isDie = false){
	    foreach ($data as $key=>$v){
			if(trim($v)==''){
				if($isDie)die("{'status':-1,'key':'$key'}");
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 输入sql调试信息
	 */
	public function logSql($m){
		echo $m->getLastSql();
	}
    /**
	 * 获取一行记录
	 */
	public function queryRow($sql){
		$plist = $this->query($sql);
		return empty($plist)?array():$plist[0];
	}
	

	/**
	 * 格式化查询语句中传入的in 参与，防止sql注入
	 * @param unknown $split
	 * @param unknown $str
	 */
	public function formatIn($split,$str){
		if(is_array($str)){
			$strdatas = $str;
		}else{
			$strdatas = explode($split,$str);
		}
		$data = array();
		for($i=0;$i<count($strdatas);$i++){
			$data[] = (int)$strdatas[$i];
		}
		$data = array_unique($data);
		return implode($split,$data);
	}

	/*********************************************************************************************
	 *                                      以下为WSTMall扩展函数                                                                                                          *
	 *********************************************************************************************/
	/**
	 * 分页函数
	 * @access public
	 * @param string $sql sql语句
	 * @param int $page 页码
	 * @param int $pageSize 每页条数
	 * @return array('total','pageSize','start','root','totalPage','currPage');
	 */
	public function pageQuery($sql,$start= 0,$pageSize = 10){
		$pageSize = (intval($pageSize)==0)?C('PAGE_SIZE'):$pageSize;
		if($pageSize==0)return array();
//		$page = (intval($page)<=0)?I(C('VAR_PAGE'),1):intval($page);
//		$page = ($page<=0)?1:$page;
//		$start = ($page-1)*$pageSize;
		$pager = array();
		$result = array();
		//查询总数
		$totalSql = '';
		//if(stripos(strtolower($sql),'distinct')!==false || stripos(strtolower($sql),'group')!==false || stripos(strtolower($sql),'union')!==false){
		$totalSql = "select count(1) counts from (".$sql.") as a";
		//}else{
		//$findNum = stripos($sql,' from ');
		//$totalSql = "select count(*) counts from (".substr($sql,$findNum,strlen($sql)).") as a";
		//}
		$total = $this->query($totalSql);
		//查询数据
		$result = $this->query($sql." limit ".$start.",".$pageSize);
		//计算页码信息
		$pager['total'] = $total[0]['counts'];
		$pager['pageSize'] = $pageSize;
		$pager['start'] = $start;
		$pager['root'] = $result;
		$pager['totalPage'] = ($pager['total']%$pageSize==0)?($pager['total']/$pageSize):(intval($pager['total']/$pageSize)+1);
//		$pager['currPage'] = $page;
		return $pager;
	}
};
?>